//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: handle.c 757 2007-05-10 08:19:31Z roger $
//

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <stdio.h>

#include <unihan_im.h>
#include <SWTable.h>

#include "im_config.h"
#include "candidates.h"
#include "context.h"
#include "table.h"
#include "core.h"
#include "aux.h"

int
handle_key_event(UnihanIM *uim, IMKeyEvent *kev)
{
#if DEBUG
	fprintf(stderr, "table : handle_key_event()\n");
#endif
	KeyType key_type = get_key_type(uim, kev);

	switch (key_type) {
		case KT_PREEDIT:
			return key_event_cat(uim, kev);
		case KT_PUNCTUATION:
			return key_punctuation(uim, kev);
		case KT_KICK:
			return key_event_kick(uim, kev);
		case KT_COMMIT:
			return key_event_commit(uim, kev);
		case KT_PAGE_UP:
			return page_up(uim);
		case KT_PAGE_DOWN:
			return page_down(uim);
		case KT_SEL_UP:
			return sel_up(uim);
		case KT_SEL_DOWN:
			return sel_down(uim);
		case KT_MOVE_CURSOR_BEFORE:
			return move_cursor_before(uim);
		case KT_MOVE_CURSOR_AFTER:
			return move_cursor_after(uim);
		case KT_MOVE_CURSOR_FIRST:
			return move_cursor_first(uim);
		case KT_MOVE_CURSOR_LAST:
			return move_cursor_last(uim);
		case KT_PROPERTY:
			return key_event_property(uim, kev);
		case KT_EAT:
			return TRUE;
		case KT_PROPAGATE:
		default:
			return FALSE;
	}
}

int
handle_switch_event(UnihanIM *uim, IMSwitchEvent *sev)
{
	TableSession *session = (TableSession *)uim->sessionUserData;
#if DEBUG
	fprintf(stderr, "table : handle_switch_event()\n");
#endif
	if (sev->on) {
		//get_properties(uim);
		//update_status(uim);
		//lookup_draw(uim);
		//preedit_draw(uim);
	} else {
		//properties_done(uim);
		//update_status(uim);
		lookup_done(uim);
		preedit_erase(uim);
		context_done(session->context);
	}
	return TRUE;
}

int
handle_focus_event(UnihanIM *uim, IMFocusEvent *fev)
{
#if DEBUG
	fprintf(stderr, "table : handle_focus_event()\n");
#endif
	TableSession *session = (TableSession *)uim->sessionUserData;
        if (fev->focused) {
		//properties_get(uim);
		//properties_draw(uim);
		if (session->context) {
			if (session->context->sentence) {
				preedit_draw(uim);
				if (!session->context->currNode->kicked) {
					lookup_draw(uim);
					aux_draw(uim);
				}
			}
		}
        } else {
		//properties_done(uim);
                preedit_erase(uim);
                lookup_done(uim);
		aux_done(uim);
        }
        return TRUE;
}

int
handle_config_change_event(UnihanIM *uim, IMConfigChangeEvent *ev)
{
#if DEBUG
	fprintf(stderr, "table : handle_config_change_event()\n");
#endif
	return TRUE;
}

#if NEW_PROPERTY
static int
handle_property_toggled_event(UnihanIM * uim, IMPropertyToggledEvent * ev)
{
	TableSession *session = (TableSession *)uim->sessionUserData;
	TableConfig *config = session->config;
	UnihanPropertyList *new_property;
	
	new_property = (UnihanPropertyList *)malloc(sizeof(UnihanPropertyList));
	memset(new_property, 0, sizeof(UnihanPropertyList));
	new_property->propertiesNum = 1;
	new_property->properties = (UnihanProperty *)malloc(sizeof(UnihanProperty));
	memset(new_property->properties, 0, sizeof(UnihanProperty));

	if(!strcmp(ev->key, "/PropertyRoot/width")) 
	{
		if(config->FullShape)
		{
			config->FullShape = 0;
			
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = "半";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换半角/全角模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}else{
			config->FullShape = 1;
			
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = "全";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换半角/全角模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}
		
		properties_update(uim, new_property);
		
	}else if(!strcmp(ev->key, "/PropertyRoot/puncce")) 
	{
		if (config->PuncMode) {
			config->PuncMode = 0;
						
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = ". ";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换中/英文标点模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}
		else{
			config->PuncMode = 1;
						
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = "。";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换中/英文标点模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}
		
		properties_update(uim, new_property);
		
	}else if(!strcmp(ev->key, "/PropertyRoot/state")) 
	{
		if (config->ChMode) {
			config->ChMode = 0;
						
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = "英";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换中/英文输入模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}
		else{
			config->ChMode = 1;
						
			new_property->properties[0].key = ev->key;
			new_property->properties[0].label = "中";
			new_property->properties[0].icon = NULL;
			new_property->properties[0].tip = "切换中/英文输入模式";
			new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
		}
		
		properties_update(uim, new_property);
	}
	
	return 0;
}

static int
handle_property_register_event(UnihanIM * uim, IMPropertyRegisterEvent * ev)
{
	TableSession *session = (TableSession *)uim->sessionUserData;
	TableConfig *config = session->config;
	UnihanPropertyList *new_property;
	
	new_property = (UnihanPropertyList *)malloc(sizeof(UnihanPropertyList));
	memset(new_property, 0, sizeof(UnihanPropertyList));
	new_property->propertiesNum = 4;
	new_property->properties = (UnihanProperty *)malloc(4 * sizeof(UnihanProperty));
	memset(new_property->properties, 0, 4*sizeof(UnihanProperty));
	
	new_property->properties[0].key = "/PropertyRoot";
	new_property->properties[0].label = NULL;
	new_property->properties[0].icon = NULL;
	new_property->properties[0].tip = NULL;
	new_property->properties[0].state = UNIHAN_PROPERTY_ACTIVE;
	
	config->ChMode = 1;
	new_property->properties[1].key = "/PropertyRoot/state";
	new_property->properties[1].label = "中";
	new_property->properties[1].icon = NULL;
	new_property->properties[1].tip = "切换中/英文输入模式";;
	new_property->properties[1].state = UNIHAN_PROPERTY_ACTIVE;
	
	config->FullShape = 0;
	new_property->properties[2].key = "/PropertyRoot/width";
	new_property->properties[2].label = "半";
	new_property->properties[2].icon = NULL;
	new_property->properties[2].tip = "切换半角/全角模式";;
	new_property->properties[2].state = UNIHAN_PROPERTY_ACTIVE;
	
	config->PuncMode = 1;
	new_property->properties[3].key = "/PropertyRoot/puncce";
	new_property->properties[3].label = "。";
	new_property->properties[3].icon = NULL;
	new_property->properties[3].tip = "切换中/英文标点模式";
	new_property->properties[3].state = UNIHAN_PROPERTY_ACTIVE;
	
	properties_register(uim, new_property, ev->imid);
	
	return 0;
}

#endif


int
handle_symbol_user_table_event(UnihanIM *uim, IMUserTableEvent *tev)
{
	/*TableDesktopList *desktop = (TableDesktopList *)uim->desktopUserData;
	if (desktop->sut)
		free(desktop->sut);
	desktop->numSut = tev->numItem;
	desktop->sut = (SymbolUserTable *)tev->table;*/
	return TRUE;
}

int
handle_word_user_table_event(UnihanIM *uim, IMUserTableEvent *tev)
{
	/*TableDesktopList *dd = (TableDesktopList *)uim->desktopUserData;
	if (desktop->wut)
		free(desktop->wut);
	desktop->numWut = tev->numItem;
	desktop->wut = (WordUserTable *)tev->table;*/
	return TRUE;
}

// vi:ts=4:nowrap:cin:expandtab
